package com.xinzhi.service.impl;

import com.xinzhi.mapper.RealNameAuthenticationMapper;
import com.xinzhi.pojo.Idnumber;
import com.xinzhi.pojo.dto.Certificate;
import com.xinzhi.pojo.dto.Massage;
import com.xinzhi.service.IRealNameAuthenticationService;
import com.xinzhi.service.IVerifyService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RealNameAuthenticationServiceImpl implements IRealNameAuthenticationService {
    @Autowired
    private RealNameAuthenticationMapper realNameAuthenticationMapper;
    @Autowired
    private IVerifyService verifyService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Result authenticate(Certificate certificate) {
        if (certificate==null)
            return Result.error("认证失败");
        Boolean aBoolean = realNameAuthenticationMapper.selectByCertificate(certificate);
        if(aBoolean==null)
            return Result.error("认证失败");
        return Result.ok("身份信息正确");
    }

    @Override
    public Result authenticate2(Certificate certificate) {
        if (certificate==null)
            return Result.error("信息不能为空");
        Idnumber aaa = realNameAuthenticationMapper.selectByNameAndIdCard(certificate);
        if (aaa==null)
            return Result.error("认证失败");
        return Result.ok(aaa);
    }

    @Override
    public Result addUser(Massage massage) {
        if (massage==null)
            return Result.error("信息不能为空");
        if (!verifyService.isValidIdCode(massage.getIdentityCard()))
            return Result.error("身份证号码不正确");
        int i = realNameAuthenticationMapper.updateUser(massage);
        if (i<1)
            return Result.error("添加失败");
        return Result.ok("添加成功");
    }

    @Override
    public void deleteRedis(String phone) {
        redisTemplate.delete("user"+phone);
    }
}
